//package com.djt.book_system.service;
//
//import com.djt.book_system.dao.LoginDao;
//import com.djt.book_system.entity.Users;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Component;
//
//import java.util.ArrayList;
//import java.util.List;
//
//@Component
//public class LoginService implements UserDetailsService {
//    @Autowired
//    private LoginDao loginDao;
//    @Override
//    public UserDetails loadUserByUsername(String uname) throws UsernameNotFoundException {
//        Users users = loginDao.selectUser(uname);
//        if (users == null){
//            throw new UsernameNotFoundException("用户不存在");
//        }
//        //角色集合
//        List<GrantedAuthority> authorities = new ArrayList<>();
//        //获取查询到的权限
//        int aid = loginDao.selectAidByUid(users.getUid());
//        //获取向下覆盖的权限
//        int minRole = 3;//权限最低的aid
//        if (aid<minRole){
//            for (int a=minRole ;a>aid;a--){
//                authorities.add(new SimpleGrantedAuthority("ROLE_"+a));
//            }
//        }
//        //权限集合中添加权限本体
//        authorities.add(new SimpleGrantedAuthority("ROLE_"+aid));
//
//        return new User(users.getUname(),users.getUpwd(),authorities);
//    }
//}
